package com.group1.realtime.app;

import com.group1.realtime.util.FlinkSourceUtil;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.runtime.state.storage.JobManagerCheckpointStorage;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public abstract class BaseAppV1 {

    protected abstract void handle(StreamExecutionEnvironment env,
                                   DataStreamSource<String> stream);

    public void init(int port, int p, String groupIdAndCkPath, String topic) {
        System.setProperty("HADOOP_USER_NAME", "atguigu");
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", port);  // 端口的配置, 只在本地运行的时候有效
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(p);  // 并行度的设置: source 一般要和kafka的topic的分区数保持一致

        // 开启checkpointp
        env.enableCheckpointing(5000);
        env.setStateBackend(new HashMapStateBackend());
        //env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/gmall/" + groupIdAndCkPath);
        env.getCheckpointConfig().setCheckpointStorage(new JobManagerCheckpointStorage());

        env.getCheckpointConfig().setCheckpointTimeout(30000);
        env.getCheckpointConfig().setMaxConcurrentCheckpoints(1);
        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(500);
//        env.getCheckpointConfig().setExternalizedCheckpointCleanup(CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION);

        DataStreamSource<String> stream = env.addSource(FlinkSourceUtil.getKafkaSource(groupIdAndCkPath, topic));

        handle(env, stream);

        try {
            env.execute(groupIdAndCkPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}